package com.qiaoba.module.job.runner;

import com.qiaoba.module.job.entity.SysJob;
import com.qiaoba.module.job.entity.param.SysJobParam;
import com.qiaoba.module.job.service.SysJobService;
import com.qiaoba.module.job.util.ScheduleUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Scheduler;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 项目启动后-将数据库任务加载到Quzrtz中
 *
 * @author ailanyin
 * @date 2023-09-06 21:05:19
 */
@Component
@RequiredArgsConstructor
@Slf4j
public class InitSysJobRunner implements ApplicationRunner {

    private final Scheduler scheduler;
    private final SysJobService sysJobService;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        log.info("初始化定时任务开始");
        scheduler.clear();
        List<SysJob> jobList = sysJobService.selectList(new SysJobParam());
        for (SysJob sysJob : jobList) {
            log.info("正在处理ID: {} 的定时任务", sysJob.getJobId());
            ScheduleUtil.createScheduleJob(scheduler, sysJob);
        }
        log.info("初始化定时任务完成");
    }
}
